package la.iok.hzsvn.lewin.movie.shopmovie.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import la.iok.hzsvn.lewin.movie.core.RoleEnum;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.model.SimpleOrgUnitVo;
import la.iok.hzsvn.lewin.movie.core.model.SimpleUser;
import la.iok.hzsvn.lewin.movie.core.model.UserVo;
import la.iok.hzsvn.lewin.movie.services.MovieServiceInvoker;
import la.iok.hzsvn.lewin.movie.shopmovie.entity.ShopPlayer;
import la.iok.hzsvn.lewin.movie.shopmovie.mapper.ShopPlayerMapper;
import la.iok.hzsvn.lewin.movie.shopmovie.model.ShopPlayerVo;
import la.iok.hzsvn.lewin.mybatis.model.PageParam;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.lewin.mybatis.model.SortParam;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.phone.PhoneNumberUtils;
import org.springframework.stereotype.Service;

@Service
public class ShopPlayerServiceImpl extends BaseServiceImpl<ShopPlayer, ShopPlayerMapper, Long> implements ShopPlayerService{

    private final MovieServiceInvoker movieServiceInvoker;
    public ShopPlayerServiceImpl(ShopPlayerMapper mapper,MovieServiceInvoker movieServiceInvoker) {
        super(mapper);
        this.movieServiceInvoker = movieServiceInvoker;
    }

    @Override
    public Long save(@NotNull SimpleUser player, @NotNull Long orgUnitId) {
        ShopPlayer entity = executeSelect("userId=" + player.getId() + " and orgUnitId=" + orgUnitId + " limit 1");
        if(entity == null){
            entity = new ShopPlayer();
            entity.setOrgUnitId(orgUnitId);
            entity.setUserId(player.getId());
            insertSelective(entity);
        }
        return entity.getId();
    }
    @Override
    public Long save(@NotNull Long id, @NotNull Long orgUnitId) {
        SimpleOrgUnitVo orgUnit = movieServiceInvoker.orgUnitInfo(orgUnitId);
        UserVo userVo=movieServiceInvoker.userInfo(id);
        ErrorCode.RECORD_NOT_EXIST.assertNotNull(id,userVo);
        ErrorCode.ORG_UNIT_NOT_EXIST.assertNotNull(orgUnit,orgUnitId);
        ErrorCode.PROPERTY_CHECK_ERROR.assertTrue(orgUnit.shopType(), "组织","只能是店铺才能执行此操作");
        ErrorCode.PROPERTY_CHECK_ERROR.assertTrue(orgUnit.normalStatus(),"店铺","店铺已经下线");
        ShopPlayer entity = executeSelect("userId=" + id + " and orgUnitId=" + orgUnitId + " limit 1");
        if(entity == null){
            entity = new ShopPlayer();
            entity.setOrgUnitId(orgUnitId);
            entity.setUserId(id);
            insertSelective(entity);
        }
        return entity.getId();

    }
    @Override
    public PageVo<ShopPlayerVo> page(@NotNull Token token, @NotNull PageParam pageParam, @NotNull SortParam sortParam,
                                     @Nullable String phoneNumber, @Nullable String nickname, String text) {
        String orderColumn = sortParam.orderColumn("p.orgUnitId");
        if(!orderColumn.startsWith("p.")){
            orderColumn = "p." + orderColumn;
        }
        String order = sortParam.order("ASC");
        Long orgUnitId = null;
        if(!token.hasAuthority(RoleEnum.Admin)){
            orgUnitId = token.getOrgUnitId();
        }
        Page<ShopPlayerVo> list = PageHelper.startPage(pageParam.getPage(),pageParam.getLimit());
        mapper.listByParam(orderColumn, order, orgUnitId,phoneNumber, nickname, text);
     //   list.forEach(item->item.setPhoneNumber(PhoneNumberUtils.encPhoneNumber(item.getPhoneNumber())));
        return page(list, list);
    }
}
